package com.ruoyi.linkage.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.github.pagehelper.PageInfo;
import com.ruoyi.linkage.Dto.LinkageEventDto;
import com.ruoyi.linkage.Dto.LinkageEventListDto;
import com.ruoyi.linkage.Vo.LinkageEventVo;
import com.ruoyi.linkage.domain.LinkageEventRecord;
import com.ruoyi.linkage.domain.LinkageEventSpecialRecord;
import com.ruoyi.linkage.domain.LinkageStrategy;
import com.ruoyi.linkage.mapper.LinkageEventRecordMapper;
import com.ruoyi.linkage.mapper.LinkageEventSpecialRecordMapper;
import com.ruoyi.linkage.service.ILinkageStrategyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.linkage.mapper.LinkageEventMapper;
import com.ruoyi.linkage.domain.LinkageEvent;
import com.ruoyi.linkage.service.ILinkageEventService;

/**
 * 联动事件Service业务层处理
 *
 * @author pengjie
 * @date 2023-12-20
 */
@Service
public class LinkageEventServiceImpl implements ILinkageEventService
{
    @Autowired
    private LinkageEventMapper linkageEventMapper;
    @Autowired
    private LinkageEventRecordMapper linkageEventRecordMapper;
    @Autowired
    private LinkageEventSpecialRecordMapper linkageEventSpecialRecordMapper;
    @Autowired
    private ILinkageStrategyService linkageStrategyService;

    /**
     * 查询联动事件
     *
     * @param linkageEventId 联动事件主键
     * @return 联动事件
     */
    @Override
    public LinkageEvent selectLinkageEventByLinkageEventId(String linkageEventId)
    {
        return linkageEventMapper.selectLinkageEventByLinkageEventId(linkageEventId);
    }

    /**
     * 查询联动事件列表
     *
     * @param linkageEvent 联动事件
     * @return 联动事件
     */
    @Override
    public List<LinkageEvent> selectLinkageEventList(LinkageEvent linkageEvent)
    {
        return linkageEventMapper.selectLinkageEventList(linkageEvent);
    }

    /**
     * 新增联动事件
     *
     * @param linkageEvent 联动事件
     * @return 结果
     */
    @Override
    public int insertLinkageEvent(LinkageEvent linkageEvent)
    {
        return linkageEventMapper.insertLinkageEvent(linkageEvent);
    }

    /**
     * 修改联动事件
     *
     * @param linkageEvent 联动事件
     * @return 结果
     */
    @Override
    public int updateLinkageEvent(LinkageEvent linkageEvent)
    {
        return linkageEventMapper.updateLinkageEvent(linkageEvent);
    }

    /**
     * 批量删除联动事件
     *
     * @param linkageEventIds 需要删除的联动事件主键
     * @return 结果
     */
    @Override
    public int deleteLinkageEventByLinkageEventIds(String[] linkageEventIds)
    {
        return linkageEventMapper.deleteLinkageEventByLinkageEventIds(linkageEventIds);
    }

    /**
     * 删除联动事件信息
     *
     * @param linkageEventId 联动事件主键
     * @return 结果
     */
    @Override
    public int deleteLinkageEventByLinkageEventId(String linkageEventId)
    {
        return linkageEventMapper.deleteLinkageEventByLinkageEventId(linkageEventId);
    }

    @Override
    public LinkageEventDto selectLinkageEventListDto() {
        PageInfo<LinkageEvent> page1=new PageInfo<>(linkageEventMapper.selectLinkageEventList(new LinkageEvent()));
        PageInfo<LinkageEventRecord> page2=new PageInfo<>(linkageEventRecordMapper.selectLinkageEventRecordList(new LinkageEventRecord()));
        PageInfo<LinkageEventSpecialRecord> page3 =new PageInfo<>(linkageEventSpecialRecordMapper.selectLinkageEventSpecialRecordList(new LinkageEventSpecialRecord()));
        LinkageEventDto linkageEventDto =new LinkageEventDto();
        linkageEventDto.setTotalCount(String.valueOf(page1.getTotal()));
        linkageEventDto.setResponseTotalCount(String.valueOf(page2.getTotal()));
        linkageEventDto.setSpecialCount(String.valueOf(page3.getTotal()));
        return linkageEventDto;
    }

    /**
     * 条件查询联动事件
     *
     * @param linkageEventVo 联动事件主键
     * @return 联动事件
     */
    @Override
    public List<LinkageEventListDto> queryLinkageEventList(LinkageEventVo linkageEventVo) {
        List<LinkageEvent> list =linkageEventMapper.queryLinkageEventList(linkageEventVo);
        List<LinkageEventListDto> listDto=list.stream().map(it->{
            LinkageEventListDto linkageEventListDto=new LinkageEventListDto();
            BeanUtils.copyProperties(it,linkageEventListDto);
            LinkageStrategy linkageStrategy=linkageStrategyService.selectLinkageStrategyByLinkageStrategyId(it.getLinkageStrategyId());
            if (linkageStrategy!=null){
                linkageEventListDto.setLinkageStrategyName(
                        linkageStrategy.getLinkageStrategyName());
            }
            return linkageEventListDto;
        }).collect(Collectors.toList());
        return listDto;
    }
}
